import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import UserInfo from './views/UserInfo.vue'
import Logout from './views/Logout.vue'
import axios from './http'
import utils from './utils/utils'
Vue.use(Router)
const routes = [
    // 空地址跳转至home页面
    {
        path: '/',
        component: Home
    },
    // 首页城市列表
    {
        name: 'home',
        path: '/home',
        component: Home
    },
    // 当前选择城市页面
    {
        name: 'userinfo',
        path: '/userinfo',
        component: UserInfo
    }, {
        name: 'userinfob',
        path: '/userinfob',
        component: () => import(/* webpackChunkName: 'city' */ `@/views/UserInfo.vue`)
    }, {
        name: 'logout2',
        path: '/logout2',
        //component: Logout,
        beforeEnter(to, from, next) {
            console.log("进入logout")
            var ttoken=null;
            if(ttoken=localStorage.getItem('token')){
                //localStorage.removeItem('token');
                axios({
                    methods:'get',
                    url:'/api/logout2',
                    headers:{
                         
                         'UserToken': ttoken
                    },
                    params: {
                        token: ttoken,
                    }
                }).then(res=>{
                    if(res.data.code=='200'){
                        localStorage.removeItem('token');
                        utils.delCookie('nickname');
                        window.location.href="/";
                    }
                })
                
            }else{
                window.location.href="/";
            }


            
            //next({name:'home'})
            
        }
    },
    //{
     //   path: '*',
      //  redirect: '/'
    //},
]
const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
});
router.beforeEach((to, from, next) => {
    const hasToken = localStorage.token ? true : false;
    // if (to.path == "/userinfo") {
    //     if (hasToken) {
    //         axios.get("/api/checkToken", {
    //             params: {
    //                 'token': localStorage.token,
    //             }
    //         }).then(function (response) { })
    //     } else {
    //         window.location.href = "http://www.sso.com:5000/userinfo?redirect=" + window.location.href + to.fullPath;
    //     }
    //     //next();
    //     //next(false) 
    // }
    
    next();
})
export default router
